package com.loveprogrammer.rocketmq.lesson1.producer;

import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.common.message.Message;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName SimpleBatchProducer
 * @Description 这里调用非常简单，将消息打包成 Collection<Message> msgs
 * 传入方法中即可，需要注意的是批量消息的大小不能超过 1MiB（否则需要自行分割），其次同一批 batch 中 topic 必须相同。
 * @Author admin
 * @Date 2023/11/1 17:25
 * @Version 1.0
 */
public class SimpleBatchProducer {
    public static void main(String[] args) throws Exception {
        DefaultMQProducer producer = new DefaultMQProducer("batch_group");
        producer.setNamesrvAddr("211.149.243.54:9800");
        producer.start();

        //If you just send messages of no more than 1MiB at a time, it is easy to use batch
        //Messages of the same batch should have: same topic, same waitStoreMsgOK and no schedule support
        String topic = "TopicTest";
        List<Message> messages = new ArrayList<>();
        messages.add(new Message(topic, "TagSB", "OrderID001", "Hello world 0".getBytes()));
        messages.add(new Message(topic, "TagSB", "OrderID002", "Hello world 1".getBytes()));
        messages.add(new Message(topic, "TagSB", "OrderID003", "Hello world 2".getBytes()));

        producer.send(messages);
    }
}
